class Uv < Formula
  desc "Extremely fast Python package installer and resolver, written in Rust"
  homepage "https://docs.astral.sh/uv/"
  url "https://github.com/astral-sh/uv/archive/refs/tags/0.6.3.tar.gz"
  sha256 "3d91f49e70498947ef70ccc562017e75f867a5a8dc1dcb1386f702c613b6678b"
  license any_of: ["Apache-2.0", "MIT"]
  head "https://github.com/astral-sh/uv.git", branch: "main"

  bottle do
    sha256 cellar: :any_skip_relocation, arm64_sequoia: "2017a09e62313c5d2c2548be86b382e2afa8b7873f5377a0d956dbfe37ac623c"
    sha256 cellar: :any_skip_relocation, arm64_sonoma:  "f4f70429926ee02afda8281d7b42212919a94f0f0b76ddd0f9eef4b0362717fa"
    sha256 cellar: :any_skip_relocation, arm64_ventura: "7abea9c0b4c8d85a496e7b72b374def3c21a0f609c802045555f6cc3758d65b9"
    sha256 cellar: :any_skip_relocation, sonoma:        "a9d03c34f46adaeea627fdcd8498b482f3af4a955aa4519edcabc48acf7932b3"
    sha256 cellar: :any_skip_relocation, ventura:       "fe38631c063c71867c1adf81f19d2dcd9b86a9c5eb7f55aa7f6512431b10056b"
    sha256 cellar: :any_skip_relocation, x86_64_linux:  "cfe6c35cb5066f17fd19f7732eb091e534c95c8de37235ef36ad7c5bd02f7f43"
  end

  depends_on "pkgconf" => :build
  depends_on "rust" => :build

  uses_from_macos "python" => :test
  uses_from_macos "bzip2"
  uses_from_macos "xz"

  def install
    ENV["UV_COMMIT_HASH"] = ENV["UV_COMMIT_SHORT_HASH"] = tap.user
    ENV["UV_COMMIT_DATE"] = time.strftime("%F")
    system "cargo", "install", "--no-default-features", *std_cargo_args(path: "crates/uv")
    generate_completions_from_executable(bin/"uv", "generate-shell-completion")
    generate_completions_from_executable(bin/"uvx", "--generate-shell-completion")
  end

  test do
    (testpath/"requirements.in").write <<~REQUIREMENTS
      requests
    REQUIREMENTS

    compiled = shell_output("#{bin}/uv pip compile -q requirements.in")
    assert_match "This file was autogenerated by uv", compiled
    assert_match "# via requests", compiled

    assert_match "ruff 0.5.1", shell_output("#{bin}/uvx -q ruff@0.5.1 --version")
  end
end
